<template>
  <!-- 单位分账-->
  <div class="class_recharge_statistics">
    <el-form :inline="true" :model="elform" class="demo-form-inline" size="mini">
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="gosearch" size="mini" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="exportexeclto" size="mini">导出</el-button>
      </el-form-item>
    </el-form>
    <el-tabs type="border-card" v-model="activeName">
      <el-tab-pane label="单位分账汇总" name="second">
        <el-table
          ref="table1"
          v-loading="dataListLoading1"
          :data="ledgrList"
          border
          highlight-current-row
          :header-cell-style="headFirst"
          style="width: 100%"
          :cell-style="(res) => headFirst_col(res, [])"
        >
          <el-table-column prop="account" label="支付宝登录名"></el-table-column>
          <el-table-column prop="amountSum" label="分账金额(元)">
            <template slot-scope="scope">
              <div>{{ scope.row.amountSum | amountyuan }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="dataMakeTime" label="数据产生时间"></el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            @size-change="handleSizeChangepage1"
            @current-change="handleCurrentChangepage1"
            :current-page="currentPage1"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize1"
            layout="total, sizes, prev, pager, next, jumper"
            :total="recordCount1"
          ></el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="单位分账记录" name="first">
        <!-- 统计 -->
        <el-table
          v-loading="dataListLoading"
          :data="tableinformation"
          ref="table"
          style="width: 100%"
          border="border"
          :header-cell-style="headFirst"
          highlight-current-row
          :span-method="arraySpanMethod"
          :cell-style="(res) => headFirst_col(res, [])"
        >
          <el-table-column prop="settleNo" label="分账订单号"></el-table-column>
          <el-table-column prop="tradeNo" label="订单号"></el-table-column>
          <el-table-column prop="account" label="支付宝登录名"></el-table-column>
          <el-table-column prop="amount" label="分账金额(元)">
            <template slot-scope="scope">
              <div>{{ scope.row.amount | amountyuan }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="dataMakeTime" label="数据产生时间"></el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            @size-change="handleSizeChangepage"
            @current-change="handleCurrentChangepage"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="recordCount"
          ></el-pagination>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeName: 'second',
      // 分账统计数据
      ledgrList: [],
      dataListLoading1: false,
      currentPage1: 1,
      recordCount1: 0,
      pageSize1: 20,
      pageNum1: 1,

      elform: {},
      dataListLoading: false,
      currentPage: 1,
      recordCount: 0,
      pageSize: 20,
      pageNum: 1,
      datetime: [],
      time: [],
      tableinformation: [],
      userGuid: '',
      searchInfo: {},
      agentlistout: [],
      totalto: false,
      alladjustAmount: '',
      allmoneyCount: '',
      allmoneyAvg: '',
      hejilist: {},
      filename: '单位分账记录'
    }
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  created() {
    // 初始化时间
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
  },
  mounted() {
    this.classcspadjustWidth()
  },
  methods: {
    // 合并
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === this.tableinformation.length - 1) {
        if (columnIndex === 0) {
          return [0, 0]
        } else if (columnIndex === 1) {
          return [0, 0]
        } else if (columnIndex === 2) {
          return [1, 3]
        }
      }
    },
    // 分账统计数据查询
    getLeder() {
      this.dataListLoading1 = true
      this.$http({
        url: this.$http.adornUrl('/pay/unit-trade-settle-statistics-page'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum1,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          console.log(data.messageContent.data)
          this.ledgrList = data.messageContent.data || []
          if (this.ledgrList.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
          }
          this.recordCount1 = data.messageContent.recordCount
          this.currentPage1 = data.messageContent.pageNum
          this.dataListLoading1 = false
        }
        if (data.messageCode === 500) {
          this.$message.error('查询遇未知错误')
        }
      })
    },

    handleSizeChangepage1(val) {
      this.pageSize1 = val
      this.getLeder()
    },
    handleCurrentChangepage1(val) {
      this.pageNum1 = val
      this.getLeder()
    },
    //合计
    classcspadjustWidth() {},
    // 查询
    gosearch() {
      this.pageNum = 1
      this.searchInfo = {}
      this.searchInfo.stateTime = this.datetime[0] + ' ' + this.time[0]
      this.searchInfo.endTime = this.datetime[1] + ' ' + this.time[1]
      this.searchInfo.unitGuid = this.changeschoolid
      if (this.activeName == 'second') {
        this.getLeder()
      } else if (this.activeName == 'first') {
        this.topupclassinit()
      }
    },
    // 单位分账查询
    topupclassinit() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/pay/unit-trade-settle-page'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          this.tableinformation = data.messageContent.data || []
          if (this.tableinformation.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
          }
          this.recordCount = data.messageContent.recordCount
          this.currentPage = data.messageContent.pageNum
          this.dataListLoading = false
          this.searchtotal()
        }
        if (data.messageCode === 500) {
          this.$message.error('查询遇未知错误')
        }
      })
    },
    // 合计
    searchtotal() {
      this.$http({
        url: this.$http.adornUrl('/pay/unit-trade-settle-statistics-total'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: this.searchInfo,
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          if (data.messageCode === 200 && data.messageContent != null) {
            this.totalto = true
            console.log(data)
            // 总金额
            this.amount = data.messageContent.amountSum
            this.hejilist = {
              // 支付宝名称
              account: data.messageContent.account,
              // 数据产生时间
              dataMakeTime: data.messageContent.dataMakeTime,
              // 总金额
              amount: this.amount
            }
            this.tableinformation.push(this.hejilist)
          }
        }
        if (data.messageCode === 500) {
          this.totalto = false
        }
      })
    },
    // 分页
    handleSizeChangepage(val) {
      this.pageSize = val
      this.topupclassinit()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.topupclassinit()
    },
    // 导出
    exportexeclto() {
      this.outexecel()
    },
    outexecel() {
      if (this.activeName == 'second') {
        if (this.ledgrList.length == 0) {
          this.$message({
            message: '查询数据后导出',
            type: 'warning'
          })
          return false
        }
        this.$http({
          url: this.$http.adornUrl('/pay/unit-trade-settle-statistics-page'),
          method: 'post',
          data: this.$http.adornData({
            messageContent: {
              pageNum: 1,
              pageSize: this.recordCount1,
              searchInfo: this.searchInfo
            },
            messageType: this.messageType
          })
        }).then(({ data }) => {
          if (data.messageCode === 200) {
            let jsonArr = data.messageContent.data
            for (var i = 0; i < jsonArr.length; i++) {
              if (jsonArr[i].amountSum != null) {
                jsonArr[i].amountSum = this.amountyua(jsonArr[i].amountSum)
              }
            }
            this.list = jsonArr
            setTimeout(() => {
              this.export2ExcelLedger()
            }, 100)
          } else {
            this.$message.error('导出遇未知错误')
          }
        })
      } else if (this.activeName == 'first') {
        if (this.tableinformation.length == 0) {
          this.$message({
            message: '查询数据后导出',
            type: 'warning'
          })
          return false
        }
        this.$http({
          url: this.$http.adornUrl('/pay/unit-trade-settle-page'),
          method: 'post',
          data: this.$http.adornData({
            messageContent: {
              pageNum: 1,
              pageSize: this.recordCount,
              searchInfo: this.searchInfo
            },
            messageType: this.messageType
          })
        }).then(({ data }) => {
          if (data.messageCode === 200) {
            let jsonArr = data.messageContent.data
            jsonArr.push({...this.hejilist})
            for (var i = 0; i < jsonArr.length; i++) {
              if (jsonArr[i].amount != null) {
                jsonArr[i].amount = this.amountyua(jsonArr[i].amount)
              }
            }
            this.agentlistout = jsonArr
            setTimeout(() => {
              this.export2Excel()
            }, 100)
          } else {
            this.$message.error('导出遇未知错误')
          }
        })
      }
    },
    export2ExcelLedger() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../excel/Export2Excel')
        let multiHeader = []
        let merges = []
        const tHeader = ['支付宝登录名', '分账金额(元)', '数据产生时间']
        const filterVal = ['account', 'amountSum', 'dataMakeTime']
        const list = this.list
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader,
          header: tHeader,
          data,
          merges,
          filename: '单位分账汇总' + this.getimeto(),
          tableheader: 1
        })
      })
    },
    export2Excel() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../excel/Export2Excel')
        let multiHeader = []
        let merges = []
        const tHeader = [
          '分账订单号',
          '订单号',
          '支付宝登录名',
          '分账金额(元)',
          '数据产生时间'
        ]
        const filterVal = [
          'settleNo',
          'tradeNo',
          'account',
          'amount',
          'dataMakeTime'
        ]
        const list = this.agentlistout
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader,
          header: tHeader,
          data,
          merges,
          filename: this.filename + this.getimeto(),
          tableheader: 1
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    }
  }
}
</script>

<style lang="scss" scoped>
.class_recharge_statistics {
  padding: 10px;

  /deep/.el-table__empty-block {
    display: none;
  }

  .demo-form-inline,
  .demo-form-inline1 {
    height: 40px;
    display: flex;

    /deep/.el-form-item__label {
      padding-right: 5px;
    }

    .el-form-item {
      margin-bottom: 0px;
    }

    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 180px;
    }
  }

  .sum_footer {
    display: flex;
    display: -webkit-flex;
    line-height: 50px;
    color: #606266;
  }

  .sum_footer_unit {
    // border-top: 1px solid #ebeef5;
    // border-right: 1px solid #ebeef5;
    flex-grow: 1;
    -webkit-flex-grow: 1;
    text-indent: 10px;
    font-size: 14px;
  }

  .sum_footer_unit.center {
    text-align: center;
    padding-left: 1px;
  }

  .sum_footer.xiaoji {
    border-bottom: 1px solid #ebeef5;
  }

  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
